package com.tongjie.hotelbooking.controller.admin;

import com.tongjie.hotelbooking.dto.UserDto;
import com.tongjie.hotelbooking.user.entity.User;
import com.tongjie.hotelbooking.user.enums.UserType;
import com.tongjie.hotelbooking.user.service.UserService;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Optional;

@RestController
@RequestMapping("/admin/merchant")
public class AdminMerchantController {

    @Resource
    private UserService userService;

    @GetMapping("/find")
    public Page<UserDto> findUser(UserDto userDto, Pageable pageable) {
        return this.userService.findUser(
                Example.of(Optional.ofNullable(userDto).map(input -> User.builder()
                        .id(input.getId())
                        .username(input.getUsername())
                        .password(input.getPassword())
                        .nickName(input.getNickName())
                        .avatar(input.getAvatar())
                        .userType(UserType.MERCHANT)
                        .userStatus(input.getUserStatus())
                        .mobile(input.getMobile())
                        .uuid(input.getUuid())
                        .wxOpenId(input.getWxOpenId())
                        .registerIp(input.getRegisterIp())
                        .lastLoginIp(input.getLastLoginIp())
                        .build()).get()
                ), pageable)
                .map(input -> UserDto.builder()
                        .id(input.getId())
                        .username(input.getUsername())
                        .password(input.getPassword())
                        .nickName(input.getNickName())
                        .avatar(input.getAvatar())
                        .userType(input.getUserType())
                        .userType(input.getUserType())
                        .userStatus(input.getUserStatus())
                        .mobile(input.getMobile())
                        .uuid(input.getUuid())
                        .wxOpenId(input.getWxOpenId())
                        .registerIp(input.getRegisterIp())
                        .lastLoginIp(input.getLastLoginIp())
                        .build());

    }

}
